package fhq.hcmute.edu.vn.gestures;

import java.util.Locale;


/**
 * @author Hien Nguyen Oct 23, 2012
 */
public class TouchPoint {
	private float x;
	private float y;

	public TouchPoint(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public TouchPoint() {
		this.x = 0;
		this.y = 0;
	}

	public TouchPoint(TouchPoint finger) {
		this.x = finger.x;
		this.y = finger.y;
	}

	public final float getX() {
		return x;
	}

	public final void setX(float x) {
		this.x = x;
	}

	public final float getY() {
		return y;
	}

	public final void setY(float y) {
		this.y = y;
	}

	public final float values() {
		return (this.x + this.y);
	}

	public static final TouchPoint subtract(TouchPoint lhs, TouchPoint rhs) {
		return new TouchPoint(lhs.x - rhs.x, lhs.y - rhs.y);
	}

	public final TouchPoint add(TouchPoint value) {
		this.x += value.getX();
		this.y += value.getY();
		return this;
	}

	public static float getSignedAngleBetween(TouchPoint a, TouchPoint b) {
		TouchPoint na = getNormalized(a);
		TouchPoint nb = getNormalized(b);

		return (float) (Math.atan2(nb.y, nb.x) - Math.atan2(na.y, na.x));
	}

	public static TouchPoint getNormalized(TouchPoint v) {
		float l = v.getLength();
		if (l == 0) {
			return new TouchPoint();
		} else {
			return new TouchPoint(v.x / l, v.y / l);
		}

	}

	public final float getLength() {
		return (float) Math.sqrt(x * x + y * y);
	}

	public final TouchPoint set(TouchPoint other) {
		x = other.getX();
		y = other.getY();
		return this;
	}

	public final boolean isZero() {
		if (x == 0 && y == 0) {
			return true;
		}
		return false;
	}

	@Override
	public final String toString() {
		// TODO Auto-generated method stub
		return String.format(Locale.US,"X=%f, Y=%f", this.x, this.y);
	}
}